using UnityEngine;
using System.Collections;

public class Joana : MonoBehaviour {


	public int velX = 5;
	
	public int velY = 1;
	
	public int death = 0;
	
	public float jump = 10;
	
	public float stageBeginX;
	
	public float stageBeginY;
	
	private bool podePular;
	
	private bool falling;
	
	private bool goUp;
	
	private bool Won;
	private bool morreu;
	

		
	// Use this for initialization
	void Start () 
	{
		//transform.position = new Vector3(stageBeginX,stageBeginY,0);
		
		podePular = true;
		
		stageBeginX = 0;
		stageBeginY = 1;
		
		falling = false;
		goUp = false;
		
	}
	
	// Update is called once per frame
	void Update () 
	{
		//movimento horizontal do personagem.
		float moveInput = Input.GetAxis ("Horizontal") * Time.deltaTime * velX;				
		transform.position += new Vector3(moveInput,0,0);
			
		
		
		if(Input.GetKeyDown(KeyCode.UpArrow) && podePular)
		{
			rigidbody.AddForce(0, jump, 0, ForceMode.Impulse);
			
			podePular = false;
		}
		
		if(morreu)
		{
			transform.position = new Vector3(stageBeginX,stageBeginY,0);
			death++;
			morreu = false;
		}
						
		if(podePular && goUp)
		{
			float moveStair = Input.GetAxis ("Vertical") * Time.deltaTime * velY;
			rigidbody.position += new Vector3 (0, moveStair, 0);
			podePular=false;
		}
		
		podePular = Physics.Raycast(transform.position, Vector3.down, 0.5f);
		
//		if (transform.position.z != 0 )
//		{
//			float zPos = Mathf.Clamp(0, 0, transform.position.z);
//			
//			transform.position = new Vector3(0, 0, zPos);
//		
//		if(transform.position.y == -2)
//		{
//			
//			transform.position = new Vector3(stageBeginX,stageBeginY,0);
//			death++;
//			
//		}
		
		Getfall ();
				
		if(falling == true)
		{
			morreu = true;
			falling = false;
			
			
		}
		
		
		
	}
	
	
	void Getfall()
	{
	
		if(transform.position.y <= -2)
		{
			
			falling = true;
			
		}
		
	}
	
	
	
	
	
	void OnGUI()
	{
		if (death >= 1)
		{
			GUILayout.Label("Mortes = " + death);
			
			
		}
		
		// Create style for a button
	    GUIStyle myButtonStyle = new GUIStyle(GUI.skin.button);
	    myButtonStyle.fontSize = 20;
		
	 
	    // Load and set Font
	    Font myFont = (Font)Resources.Load("Fonts/comic", typeof(Font));
	    myButtonStyle.font = myFont;
	 
	    // Set color for selected and unselected buttons
	    myButtonStyle.normal.textColor = Color.blue;
	    myButtonStyle.hover.textColor = Color.green;
		
		GUIStyle myBoxStyle = new GUIStyle(GUI.skin.box);
	    myBoxStyle.fontSize = 25;
		myBoxStyle.fontStyle = FontStyle.Bold;
		myBoxStyle.alignment = TextAnchor.MiddleCenter;
	 
	    // Load and set Font
	    //Font myFont = (Font)Resources.Load("Fonts/comic", typeof(Font));
	    myBoxStyle.font = myFont;
		
	 
	    // Set color for selected and unselected buttons
	    myBoxStyle.normal.textColor = Color.red;
	    myBoxStyle.hover.textColor = Color.red;
 
    
 
	    // use style in button
	    //bool testButtonTwo = GUI.Button(new Rect(10,10,50,50), "test", myButtonStyle);
		
		if(death == 3){
		
			GUI.Box (new Rect(Screen.width/2-100,Screen.height/2-100,200,50),"Game Over",myBoxStyle);
			
			if(GUI.Button(new Rect(Screen.width/2-100,Screen.height/2-50,100,100),"Menu",myButtonStyle))
			{
				
				Application.LoadLevel("Menu");
				
				
			}Time.timeScale = 0f;
			
			if(GUI.Button(new Rect(Screen.width/2,Screen.height/2-50,100,100),"Restart",myButtonStyle))
			{
				
				Application.LoadLevel(1);
				
			}Time.timeScale = 1f;
			
			
			
			
			
			
			
		}
		
	}
	
	#region colisao
	
	void OnCollisionEnter(Collision other)
	{
		if (other.collider.tag == "stair")
		{
			podePular = false;
			goUp = true;			
		}
		
		if (other.collider.tag == "stair")
		{
			morreu = true;
		}
	}	
	#endregion
	
//	void OnTriggerEnter(Collider other)
//	{
//		if (other.tag == "spikes")
//		{
//			morreu = true;
//		}
//		
//	}
	
}
